package test;

import lib.db_properties_path.*;
import java.sql.*;
import java.util.*;
import lib.dbTool.*;

public class OLDMANDETIAL extends DBconnection2 {
	public OLDMANDETIAL(SetPath set) {
		super(set);
	}

	public OLDMANDETIAL() {
		super(new DefaultSetPath());
	}

	/** Auto Increasment */
	/** Primary Key */
	/** int identity ; java.lang.Integer */
	private int idoldman;
	/** nvarchar ; java.lang.String */
	private String nameoldman;
	/** bigint ; java.lang.Long */
	private long birthdayoldman;
	/** nvarchar ; java.lang.String */
	private String teloldman;
	/** nvarchar ; java.lang.String */
	private String sexoldman;
	/** nvarchar ; java.lang.String */
	private String pictureoldman;
	/** decimal ; java.math.BigDecimal */
	private double longitudeoldman;
	/** decimal ; java.math.BigDecimal */
	private double latitudeoldman;
	/** bigint ; java.lang.Long */
	private long locationtimeoldman;
	/** int ; java.lang.Integer */
	private int idcaregiver;
	/** nvarchar ; java.lang.String */
	private String accountoldman;
	/** nvarchar ; java.lang.String */
	private String passwordoldman;

	public int getIdoldman() {
		return idoldman;
	}

	public void setIdoldman(int idoldman) {
		this.idoldman = idoldman;
	}

	public String getNameoldman() {
		return nameoldman;
	}

	public void setNameoldman(String nameoldman) {
		this.nameoldman = nameoldman;
	}

	public long getBirthdayoldman() {
		return birthdayoldman;
	}

	public void setBirthdayoldman(long birthdayoldman) {
		this.birthdayoldman = birthdayoldman;
	}

	public String getTeloldman() {
		return teloldman;
	}

	public void setTeloldman(String teloldman) {
		this.teloldman = teloldman;
	}

	public String getSexoldman() {
		return sexoldman;
	}

	public void setSexoldman(String sexoldman) {
		this.sexoldman = sexoldman;
	}

	public String getPictureoldman() {
		return pictureoldman;
	}

	public void setPictureoldman(String pictureoldman) {
		this.pictureoldman = pictureoldman;
	}

	public double getLongitudeoldman() {
		return longitudeoldman;
	}

	public void setLongitudeoldman(double longitudeoldman) {
		this.longitudeoldman = longitudeoldman;
	}

	public double getLatitudeoldman() {
		return latitudeoldman;
	}

	public void setLatitudeoldman(double latitudeoldman) {
		this.latitudeoldman = latitudeoldman;
	}

	public long getLocationtimeoldman() {
		return locationtimeoldman;
	}

	public void setLocationtimeoldman(long locationtimeoldman) {
		this.locationtimeoldman = locationtimeoldman;
	}

	public int getIdcaregiver() {
		return idcaregiver;
	}

	public void setIdcaregiver(int idcaregiver) {
		this.idcaregiver = idcaregiver;
	}

	public String getAccountoldman() {
		return accountoldman;
	}

	public void setAccountoldman(String accountoldman) {
		this.accountoldman = accountoldman;
	}

	public String getPasswordoldman() {
		return passwordoldman;
	}

	public void setPasswordoldman(String passwordoldman) {
		this.passwordoldman = passwordoldman;
	}

	public interface fn {
		String TABLE_NAME = "OLDMANDETIAL";
		String PK = "idoldman";
		String idoldman = "idoldman";
		String nameoldman = "nameoldman";
		String birthdayoldman = "birthdayoldman";
		String teloldman = "teloldman";
		String sexoldman = "sexoldman";
		String pictureoldman = "pictureoldman";
		String longitudeoldman = "longitudeoldman";
		String latitudeoldman = "latitudeoldman";
		String locationtimeoldman = "locationtimeoldman";
		String idcaregiver = "idcaregiver";
		String accountoldman = "accountoldman";
		String passwordoldman = "passwordoldman";
	}

	public String sql(String SQL) {
		return Utility.getSQL組合字(SQL, OLDMANDETIAL.fn.class);
	}

	public int clear() {
		String SQL = "delete from OLDMANDETIAL";
		Object parameter[] = {};
		return super.excuteUpdate(SQL, parameter);
	}

	public int delete(int idoldman) {
		String SQL = "delete from OLDMANDETIAL where idoldman=?";
		Object parameter[] = { idoldman };
		return super.excuteUpdate(SQL, parameter);
	}

	public int update(String nameoldman, long birthdayoldman, String teloldman,
			String sexoldman, String pictureoldman, double longitudeoldman,
			double latitudeoldman, long locationtimeoldman, int idcaregiver,
			String accountoldman, String passwordoldman, int idoldman) {
		String SQL = "update OLDMANDETIAL set nameoldman=?,birthdayoldman=?,teloldman=?,sexoldman=?,pictureoldman=?,longitudeoldman=?,latitudeoldman=?,locationtimeoldman=?,idcaregiver=?,accountoldman=?,passwordoldman=? where idoldman=?";
		Object parameter[] = { nameoldman, birthdayoldman, teloldman,
				sexoldman, pictureoldman, longitudeoldman, latitudeoldman,
				locationtimeoldman, idcaregiver, accountoldman, passwordoldman,
				idoldman };
		return super.excuteUpdate(SQL, parameter);
	}

	public int insert(String nameoldman, long birthdayoldman, String teloldman,
			String sexoldman, String pictureoldman, double longitudeoldman,
			double latitudeoldman, long locationtimeoldman, int idcaregiver,
			String accountoldman, String passwordoldman) {
		String SQL = "insert into OLDMANDETIAL(nameoldman,birthdayoldman,teloldman,sexoldman,pictureoldman,longitudeoldman,latitudeoldman,locationtimeoldman,idcaregiver,accountoldman,passwordoldman) values(?,?,?,?,?,?,?,?,?,?,?)";
		Object parameter[] = { nameoldman, birthdayoldman, teloldman,
				sexoldman, pictureoldman, longitudeoldman, latitudeoldman,
				locationtimeoldman, idcaregiver, accountoldman, passwordoldman };
		return super.excuteUpdate(SQL, parameter);
	}

	public String[][] select() {
		String SQL = "select * from OLDMANDETIAL";
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public OLDMANDETIAL[] select_Object() {
		String SQL = "select * from OLDMANDETIAL";
		Object parameter[] = {};
		String data[][] = super.select(SQL, parameter);
		OLDMANDETIAL obj[] = toObject(data);
		return obj;
	}

	public String[][] select(int idoldman) {
		String SQL = "select * from OLDMANDETIAL where idoldman=?";
		Object parameter[] = { idoldman };
		return super.select(SQL, parameter);
	}

	public OLDMANDETIAL[] select_Object(int idoldman) {
		String data[][] = this.select(idoldman);
		OLDMANDETIAL obj[] = toObject(data);
		return obj;
	}

	public int selectCount() {
		String SQL = "select count(idoldman) from OLDMANDETIAL";
		Object parameter[] = {};
		String d[][] = super.select(SQL, parameter);
		return Integer.parseInt(d[0][0]);
	}

	public String[][] selectPage(int pageSize, int pageIndex, int rowCount) {
		int desc = PaggingSQL.ASC;
		PaggingSQL ps = new lib.dbTool.MsSQL2012();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPage(pageSize, pageIndex, TABLE_NAME, PK,
				desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount) {
		PaggingSQL ps = new lib.dbTool.MsSQL2012();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	/**
	 * 關鍵字 where 不用寫
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param sortName
	 * @param desc
	 * @param rowCount
	 * @param whereCondition
	 *            = 【pk>0】, 關鍵字 where 不用寫
	 * @return
	 */
	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount, String whereCondition) {
		PaggingSQL ps = new lib.dbTool.MsSQL2012();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		// 範例,關鍵字 where 不用寫, ex:【pk>0】
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount, whereCondition);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public OLDMANDETIAL[] selectPage_Object(int pageSize, int pageIndex,
			int rowCount) {
		String data[][] = this.selectPage(pageSize, pageIndex, rowCount);
		OLDMANDETIAL obj[] = toObject(data);
		return obj;
	}

	/**
	 * 分組統計筆數
	 * 
	 * @param fk_List
	 * @return
	 */
	public Map<Integer, Integer> getGroupCount(String fkName,
			LinkedList<Integer> fk_List) {
		String q = "";
		Object parameter[] = new Object[fk_List.size()];
		int i = 0;
		for (Integer no : fk_List) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		String SQL = "";
		SQL += "SELECT " + fkName + ", count(" + fkName + ") ";
		SQL += "FROM " + fn.TABLE_NAME + " where " + fkName + " in (" + q + ")";
		SQL += "group by " + fkName + ";";
		String data[][] = super.select(SQL, parameter);
		for (int j = 0; j < data.length; j++) {
			int key = Integer.parseInt(data[j][0]);
			int count = Integer.parseInt(data[j][1]);
			map.put(key, count);
		}
		return map;
	}

	/**
	 * Where PK in(?,?,?....)
	 * 
	 * @param idList
	 * @return
	 */
	public Map<Integer, OLDMANDETIAL> selectMap(LinkedList<Integer> idList) {
		String q = "";
		Object parameter[] = new Object[idList.size()];
		int i = 0;
		for (Integer no : idList) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, OLDMANDETIAL> map = new HashMap<Integer, OLDMANDETIAL>();

		String SQL = "select * from OLDMANDETIAL where idoldman in (" + q + ")";
		String data[][] = super.select(SQL, parameter);
		OLDMANDETIAL[] obj = this.toObject(data);
		for (OLDMANDETIAL o : obj) {
			map.put(new Integer(o.idoldman), o);
		}
		return map;
	}

	public OLDMANDETIAL[] toObject(String data[][]) {
		OLDMANDETIAL[] obj = new OLDMANDETIAL[data.length];
		for (int i = 0; i < obj.length; i++) {
			obj[i] = new OLDMANDETIAL(super.getSetting());
			obj[i].idoldman = Integer.parseInt(data[i][0]);
			obj[i].nameoldman = data[i][1];
			obj[i].birthdayoldman = Long.parseLong(data[i][2]);
			obj[i].teloldman = data[i][3];
			obj[i].sexoldman = data[i][4];
			obj[i].pictureoldman = data[i][5];
			obj[i].longitudeoldman = Double.parseDouble(data[i][6]);
			obj[i].latitudeoldman = Double.parseDouble(data[i][7]);
			obj[i].locationtimeoldman = Long.parseLong(data[i][8]);
			obj[i].idcaregiver = Integer.parseInt(data[i][9]);
			obj[i].accountoldman = data[i][10];
			obj[i].passwordoldman = data[i][11];
		}
		return obj;
	}
}